云柯

文章 评论 浏览 27

云柯 2025-02-21 00:46:44

我试图在环境中复制相同的错误,并获得了与以下相同的错误:

“在此处输入图像描述”

请注意,您需要通过 应用程序对象ID 而不是应用程序ID:

此处能够成功地创建扩展属性,如下所示:

“

参考:

创建ExtensionProperty -Microsoft Graph V1.0 |微软文档

I tried to reproduce the same in my environment and got the same error as below:

enter image description here

Please note that, you need to pass Application Object ID instead of Application ID:

enter image description here

When I passed the Application Object ID, I was able create the extension property successfully like below:

enter image description here

Reference:

Create extensionProperty - Microsoft Graph v1.0 | Microsoft Docs

无法使用“ Jobrole”创建扩展属性。邮差

云柯 2025-02-20 22:46:05

添加新项目类型是很好的选择。

class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        const val ITEM_TYPE_CONTENT = 0
        const val ITEM_TYPE_BUTTON = 1
    }

    val dataList: List<String>? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == ITEM_TYPE_CONTENT) {
            // TODO return a content holder
        }else {
            // TODO return a button holder
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        TODO("Not yet implemented")
    }

    override fun getItemCount(): Int {
        return (dataList?.size ?: 0) + 1
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) {
            ITEM_TYPE_BUTTON
        } else {
            ITEM_TYPE_CONTENT
        }
    }
}

add a new itemType is good selection。

class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        const val ITEM_TYPE_CONTENT = 0
        const val ITEM_TYPE_BUTTON = 1
    }

    val dataList: List<String>? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == ITEM_TYPE_CONTENT) {
            // TODO return a content holder
        }else {
            // TODO return a button holder
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        TODO("Not yet implemented")
    }

    override fun getItemCount(): Int {
        return (dataList?.size ?: 0) + 1
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) {
            ITEM_TYPE_BUTTON
        } else {
            ITEM_TYPE_CONTENT
        }
    }
}

如何在回收器视图(Kotlin)的末端添加按钮?

云柯 2025-02-20 21:26:25

这是一个谱系问题,解决方案与我在此处发布的问题相同:使用MEATAR AS MEATAILE

This is a lineage issue and the solution is the same as I posted here: Using Measure as Variable

使用度量作为滤波器参数

云柯 2025-02-20 17:09:07

我遇到了GitHub Actions的同一问题,这仅仅是由于我对NPM的代币已过期。

I ran into the same issue with Github Actions and it was simply due to my token on npm being expired.

获取NPM错误NPM ERR! 404&#x27;@my-package@2.0.0&#x27;尝试将新的NPM软件包版本发布到NPM时不在此注册表中

云柯 2025-02-20 14:50:32

您可以将一个月分成日历周。 (例如,在2022年7月,将是:7月1-2日,3-9、10-16,ECT…)

然后,取决于当天,占据的第一周或下半年。

在过滤的几周内迭代,计算工作日。

我选择在本月上半年的第三周内包括5周,但是您可以通过更改 MATH.CEIL MATH.Floor 来更改此操作。

/**
 * Get the last item in an array, or undefined if the array is empty.
 * @template T
 * @param {[T]} array
 * @returns {T|undefined}
 */
const lastItem = array => array[array.length - 1];

const getWeekdays = current => {
    /** @type {[[Date]]} */
    const weeks = [];

    // Get the weeks
    /**
     * Get the calendar week of the given date.
     * @param {Date} firstDay The first day of the week.
     * @returns {[Date]}
     */
    const getWeek = firstDay => {
        /** @type {[Date]} */
        let days = [];

        let dateToTest = new Date(firstDay);
        // Continue until the end of the week or month, whichever comes first.
        while (
            dateToTest.getDay() <= 6 &&
            dateToTest.getMonth() == firstDay.getMonth()
        ) {
            days.push(new Date(dateToTest));
            dateToTest.setDate(dateToTest.getDate() + 1);
        }

        return days;
    };

    // The first day of the month
    const firstDay = new Date(current.getFullYear(), current.getMonth());
    let dateToTest = new Date(firstDay);
    do {
        weeks.push(getWeek(dateToTest));
        dateToTest = new Date(lastItem(lastItem(weeks)));
        dateToTest.setDate(dateToTest.getDate() + 1);
    } while (dateToTest.getMonth() == firstDay.getMonth());

    // Filter to half of the month
    // Get the week of the given date
    let currentWeek = 0;
    weekLoop: for (let i = 0; i < weeks.length; i++) {
        const week = weeks[i];
        for (const day of week) {
            if (day == current) {
                currentWeek = i;
                break weekLoop;
            }
        }
    }

    /** @type {[[Date]]} */
    let weeksInHalf = [];

    const numOfWeeksInFirstHalf = Math.ceil(weeks.length / 2),
        numOfWeeksInSecondHalf = weeks.length - numOfWeeksInFirstHalf;

    for (
        let i = 0;
        i <
        (currentWeek < numOfWeeksInFirstHalf
            ? numOfWeeksInFirstHalf
            : numOfWeeksInSecondHalf);
        i++
    ) {
        weeksInHalf.push(weeks[i]);
    }

    // Filter out weekends
    // Format dates
    return weeksInHalf
        .flat()
        .filter(day => day.getDay() > 0 && day.getDay() < 6)
        .map(
            day => `${day.getFullYear()}-${day.getMonth() + 1}-${day.getDate()}`
        );
};

// Tests
for (let i = 0; i < 12; i++) {
    const weekdays = getWeekdays(new Date(2022, i));
    weekdays.forEach(dateString => {
        const [year, month, day] = dateString.split("-");
        const date = new Date(year, month - 1, day);
        if (date.getDay() == 0 || date.getDay() == 6)
            throw new Error("Invalid day: (day)");
        else console.log(dateString)
    });
}

You could split the month up into calendar weeks. (e.g. for July 2022 it would be: July 1-2, 3-9, 10-16, ect…)

Then, depending on the day, take either the first or second half of the weeks.

Iterate over the filtered weeks, counting the weekdays.

I choose to include the third week in the first half of the month if there were 5 weeks, but you could change that by changing Math.ceil to Math.floor

/**
 * Get the last item in an array, or undefined if the array is empty.
 * @template T
 * @param {[T]} array
 * @returns {T|undefined}
 */
const lastItem = array => array[array.length - 1];

const getWeekdays = current => {
    /** @type {[[Date]]} */
    const weeks = [];

    // Get the weeks
    /**
     * Get the calendar week of the given date.
     * @param {Date} firstDay The first day of the week.
     * @returns {[Date]}
     */
    const getWeek = firstDay => {
        /** @type {[Date]} */
        let days = [];

        let dateToTest = new Date(firstDay);
        // Continue until the end of the week or month, whichever comes first.
        while (
            dateToTest.getDay() <= 6 &&
            dateToTest.getMonth() == firstDay.getMonth()
        ) {
            days.push(new Date(dateToTest));
            dateToTest.setDate(dateToTest.getDate() + 1);
        }

        return days;
    };

    // The first day of the month
    const firstDay = new Date(current.getFullYear(), current.getMonth());
    let dateToTest = new Date(firstDay);
    do {
        weeks.push(getWeek(dateToTest));
        dateToTest = new Date(lastItem(lastItem(weeks)));
        dateToTest.setDate(dateToTest.getDate() + 1);
    } while (dateToTest.getMonth() == firstDay.getMonth());

    // Filter to half of the month
    // Get the week of the given date
    let currentWeek = 0;
    weekLoop: for (let i = 0; i < weeks.length; i++) {
        const week = weeks[i];
        for (const day of week) {
            if (day == current) {
                currentWeek = i;
                break weekLoop;
            }
        }
    }

    /** @type {[[Date]]} */
    let weeksInHalf = [];

    const numOfWeeksInFirstHalf = Math.ceil(weeks.length / 2),
        numOfWeeksInSecondHalf = weeks.length - numOfWeeksInFirstHalf;

    for (
        let i = 0;
        i <
        (currentWeek < numOfWeeksInFirstHalf
            ? numOfWeeksInFirstHalf
            : numOfWeeksInSecondHalf);
        i++
    ) {
        weeksInHalf.push(weeks[i]);
    }

    // Filter out weekends
    // Format dates
    return weeksInHalf
        .flat()
        .filter(day => day.getDay() > 0 && day.getDay() < 6)
        .map(
            day => `${day.getFullYear()}-${day.getMonth() + 1}-${day.getDate()}`
        );
};

// Tests
for (let i = 0; i < 12; i++) {
    const weekdays = getWeekdays(new Date(2022, i));
    weekdays.forEach(dateString => {
        const [year, month, day] = dateString.split("-");
        const date = new Date(year, month - 1, day);
        if (date.getDay() == 0 || date.getDay() == 6)
            throw new Error("Invalid day: (day)");
        else console.log(dateString)
    });
}

根据当前日期和返回工作日(JS),分为第一和下半年

云柯 2025-02-20 12:57:07

注释十六进制编码,每对十六进制字符代表字节的价值。 128个字符 - &GT; 64字节 by jps 可以在python (1)可以照亮(并解决) >在更多方面,例如:

hexstr = '3f5b2e3acfd36066b8cb4114e706d79d270c7c4219a6312c4b40f995f138c2ba5ba2b62aa4831e0bdac8f8802626d3c4f8872b8996d8bebc5ab697e0c702a6eb'
print( len(hexstr))
print( hexstr)

bytes_object = bytes.fromhex(hexstr)

import binascii
print( bytes_object == binascii.unhexlify(hexstr))

import codecs
print( bytes_object == codecs.decode(hexstr, 'hex_codec'))

print( len(bytes_object))
print( bytes_object)

结果。\ so \ 72880674.py

128
3f5b2e3acfd36066b8cb4114e706d79d270c7c4219a6312c4b40f995f138c2ba5ba2b62aa4831e0bdac8f8802626d3c4f8872b8996d8bebc5ab697e0c702a6eb
True
True
64
b"?[.:\xcf\xd3`f\xb8\xcbA\x14\xe7\x06\xd7\x9d'\x0c|B\x19\xa61,K@\xf9\x95\xf18\xc2\xba[\xa2\xb6*\xa4\x83\x1e\x0b\xda\xc8\xf8\x80&&\xd3\xc4\xf8\x87+\x89\x96\xd8\xbe\xbcZ\xb6\x97\xe0\xc7\x02\xa6\xeb"

(1)我想您知道 python basics ,因为您的另一个问题…

Comment hexadecimal encoding, every pair of hex characters represents the value of a byte. 128 characters -> 64 bytes by jps could be illuminated (and solved) in Python(1) in more ways, e.g. as follows:

hexstr = '3f5b2e3acfd36066b8cb4114e706d79d270c7c4219a6312c4b40f995f138c2ba5ba2b62aa4831e0bdac8f8802626d3c4f8872b8996d8bebc5ab697e0c702a6eb'
print( len(hexstr))
print( hexstr)

bytes_object = bytes.fromhex(hexstr)

import binascii
print( bytes_object == binascii.unhexlify(hexstr))

import codecs
print( bytes_object == codecs.decode(hexstr, 'hex_codec'))

print( len(bytes_object))
print( bytes_object)

Result: .\SO\72880674.py

128
3f5b2e3acfd36066b8cb4114e706d79d270c7c4219a6312c4b40f995f138c2ba5ba2b62aa4831e0bdac8f8802626d3c4f8872b8996d8bebc5ab697e0c702a6eb
True
True
64
b"?[.:\xcf\xd3`f\xb8\xcbA\x14\xe7\x06\xd7\x9d'\x0c|B\x19\xa61,K@\xf9\x95\xf18\xc2\xba[\xa2\xb6*\xa4\x83\x1e\x0b\xda\xc8\xf8\x80&&\xd3\xc4\xf8\x87+\x89\x96\xd8\xbe\xbcZ\xb6\x97\xe0\xc7\x02\xa6\xeb"

(1) I suppose you know Python basics since your another questions…

这个编码类型是什么?

云柯 2025-02-20 06:26:34

所以我终于想到了一个主意。因此,我的想法是,而不是在if语句中使用if语句,而是认为这可能是为语句做多次的更好的解决方案。一个是一年,一个是一个月,一个是一天,所以我可以根据一个月的一年首先基于一年的代码进行排序,最后也是一天的。这是代码:

#include <stdio.h>

typedef struct 
{
int month;
int day;
int year;
float T_degC;
float PO4uM;
float SiO3uM;
float NO2uM;
float NO3uM;
float Salnty;
float O2ml_L;
}
hello;

int insertionSort(hello hellos[], int records);

int main()
{
FILE *file;

file = fopen("ocean.csv", "r");

if(file == NULL)
{
    printf("Error opening file. \n");
    return 1;
}

hello hellos[1405];

int read = 0;
int records = 0;   //dhladh struct

do
{
    read = fscanf(file,
                        "%d/%d/%d, %f, %f, %f, %f, %f, %f, %f",
                        &hellos[records].month,
                        &hellos[records].day,
                        &hellos[records].year,
                        &hellos[records].T_degC,
                        &hellos[records].PO4uM,
                        &hellos[records].SiO3uM,
                        &hellos[records].NO2uM,
                        &hellos[records].NO3uM,
                        &hellos[records].Salnty,
                        &hellos[records].O2ml_L);

    if(read == 10)
    {
        records++;  

    }
    if(read != 10 && feof(file) ==0)
    {
        printf("File format incorrect.\n");
        return 1;
    }
    if(ferror(file))
    {

        printf("Error reading file.\n");
        return 1;
    }

} while (!feof(file));

fclose(file);


printf("\n%d records read.\n\n", records);

insertionSort(hellos, records);
//insertionSort(hellos, records);

//Εκτυπωση των αποτελεσματων.

for(int i=0; i<records; i++)
{
    printf("\n%d, %d, %d, %f, %f, %f, %f, %f, %f, %f",
                        hellos[i].month,
                        hellos[i].day,
                        hellos[i].year,
                        hellos[i].T_degC,
                        hellos[i].PO4uM,
                        hellos[i].SiO3uM,
                        hellos[i].NO2uM,
                        hellos[i].NO3uM,
                        hellos[i].Salnty,
                        hellos[i].O2ml_L);

    printf("\n");
}
return 0;
    }

    int insertionSort(hello hellos[], int records)
    {
int i;
hello key;

for(int j=1; j<records; j++)
{
    key = hellos[j];
    i = j-1;
    if(key.year < hellos[i].year)
    {
        while(i >= 0 && (key.year < hellos[i].year))
        {
            hellos[i+1] = hellos[i];
            i = i-1;  
        }
        hellos[i+1] = key;
    }
}

for(int k=1; k<records; k++)
{
    key = hellos[k];
    i = k-1;
    if((key.month < hellos[i].month) && (key.year == hellos[i].year))
    {
        while(i >= 0 && (key.month < hellos[i].month) && (key.year == hellos[i].year))
        {
            hellos[i+1] = hellos[i];
            i = i-1;  
        }
        hellos[i+1] = key;
    }
}

for(int l=1; l<records; l++)
{
    key = hellos[l];
    i = l-1;
    if((key.day < hellos[i].day) && (key.month == hellos[i].month) && (key.year == hellos[i].year))
    {
        while(i >= 0 && (key.day < hellos[i].day) && (key.month == hellos[i].month) && (key.year == hellos[i].year))
        {
            hellos[i+1] = hellos[i];
            i = i-1;  
        }
        hellos[i+1] = key;
    }
}
return 0;
   }

So guys i finally came up with an idea. So my idea is, instead of having if statements inside of if statements, i thought it might be a better solution to make multiple for statements. One for year, one for month and one for day so i can first sort the code based on year after that also based on month and finally for day too. Here is the code:

#include <stdio.h>

typedef struct 
{
int month;
int day;
int year;
float T_degC;
float PO4uM;
float SiO3uM;
float NO2uM;
float NO3uM;
float Salnty;
float O2ml_L;
}
hello;

int insertionSort(hello hellos[], int records);

int main()
{
FILE *file;

file = fopen("ocean.csv", "r");

if(file == NULL)
{
    printf("Error opening file. \n");
    return 1;
}

hello hellos[1405];

int read = 0;
int records = 0;   //dhladh struct

do
{
    read = fscanf(file,
                        "%d/%d/%d, %f, %f, %f, %f, %f, %f, %f",
                        &hellos[records].month,
                        &hellos[records].day,
                        &hellos[records].year,
                        &hellos[records].T_degC,
                        &hellos[records].PO4uM,
                        &hellos[records].SiO3uM,
                        &hellos[records].NO2uM,
                        &hellos[records].NO3uM,
                        &hellos[records].Salnty,
                        &hellos[records].O2ml_L);

    if(read == 10)
    {
        records++;  

    }
    if(read != 10 && feof(file) ==0)
    {
        printf("File format incorrect.\n");
        return 1;
    }
    if(ferror(file))
    {

        printf("Error reading file.\n");
        return 1;
    }

} while (!feof(file));

fclose(file);


printf("\n%d records read.\n\n", records);

insertionSort(hellos, records);
//insertionSort(hellos, records);

//Εκτυπωση των αποτελεσματων.

for(int i=0; i<records; i++)
{
    printf("\n%d, %d, %d, %f, %f, %f, %f, %f, %f, %f",
                        hellos[i].month,
                        hellos[i].day,
                        hellos[i].year,
                        hellos[i].T_degC,
                        hellos[i].PO4uM,
                        hellos[i].SiO3uM,
                        hellos[i].NO2uM,
                        hellos[i].NO3uM,
                        hellos[i].Salnty,
                        hellos[i].O2ml_L);

    printf("\n");
}
return 0;
    }

    int insertionSort(hello hellos[], int records)
    {
int i;
hello key;

for(int j=1; j<records; j++)
{
    key = hellos[j];
    i = j-1;
    if(key.year < hellos[i].year)
    {
        while(i >= 0 && (key.year < hellos[i].year))
        {
            hellos[i+1] = hellos[i];
            i = i-1;  
        }
        hellos[i+1] = key;
    }
}

for(int k=1; k<records; k++)
{
    key = hellos[k];
    i = k-1;
    if((key.month < hellos[i].month) && (key.year == hellos[i].year))
    {
        while(i >= 0 && (key.month < hellos[i].month) && (key.year == hellos[i].year))
        {
            hellos[i+1] = hellos[i];
            i = i-1;  
        }
        hellos[i+1] = key;
    }
}

for(int l=1; l<records; l++)
{
    key = hellos[l];
    i = l-1;
    if((key.day < hellos[i].day) && (key.month == hellos[i].month) && (key.year == hellos[i].year))
    {
        while(i >= 0 && (key.day < hellos[i].day) && (key.month == hellos[i].month) && (key.year == hellos[i].year))
        {
            hellos[i+1] = hellos[i];
            i = i-1;  
        }
        hellos[i+1] = key;
    }
}
return 0;
   }

为什么我的插入排序只能对90%的数据进行排序?

云柯 2025-02-20 05:04:05

问题在于您要匹配这些单字符样本的部分。 \w。+需要至少两个字符要匹配。因此,对于当您获得“ E \ hich”时,第一个后斜线会匹配到Regex中的DOT,并持续到下一个后背(这是Regex的正面LookAhead部分中列出的“终结者”之一)。

您可能需要使用*而不是+

The problem is with the part you want to match those single-character samples. \w.+ requires at least two characters to match. So, for when you get "e\hich" that first backslash get matched to the dot in regex and lasts until the next backslash (which is one of the "terminators" listed in the positive lookahead portion of the regex).

You might want to use * instead of +.

正则表达式错过字符串中的比赛

云柯 2025-02-20 03:24:08

现在,我说明了读取Maven创建资源目录的字体的源代码,

scr/main/resources/calibril.ttf

Font getCalibriLightFont(int fontSize){
    Font font = null;
    try{
        URL fontURL = OneMethod.class.getResource("/calibril.ttf");
        InputStream fontStream = fontURL.openStream();
        font = new Font(Font.createFont(Font.TRUETYPE_FONT, fontStream).getFamily(), Font.PLAIN, fontSize);
        fontStream.close();
    }catch(IOException | FontFormatException ief){
        font = new Font("Arial", Font.PLAIN, fontSize);
        ief.printStackTrace();
    }   
    return font;
}

​你,享受!

Now I am illustrating the source code for reading a font from maven created resources directory,

scr/main/resources/calibril.ttf

enter image description here

Font getCalibriLightFont(int fontSize){
    Font font = null;
    try{
        URL fontURL = OneMethod.class.getResource("/calibril.ttf");
        InputStream fontStream = fontURL.openStream();
        font = new Font(Font.createFont(Font.TRUETYPE_FONT, fontStream).getFamily(), Font.PLAIN, fontSize);
        fontStream.close();
    }catch(IOException | FontFormatException ief){
        font = new Font("Arial", Font.PLAIN, fontSize);
        ief.printStackTrace();
    }   
    return font;
}

It worked for me and hope that the entire source code will also help you, Enjoy!

如何从资源文件夹加载文件?

云柯 2025-02-19 04:17:58

Hibernate搜索检测Hibernate Orm Session / EntityManager 中发生的实体更改事件。这不包括 insert /更新/ delete 您在jpql或本机SQL查询中写的语句。

限制在此处记录: https://docs.jboss.org/hibernate/stable/search/reference/en-us/html_single/#limitations-limitations-changes-in-session

也记录下来的解决方法:

一个解决方法是在运行jpql/sql查询后明确依次扣押,要么使用 massIndexer 手动

编辑:当然,您的解决方法也可能有效,如果 deletebyid 在删除它之前会加载实体(我不太熟悉Spring Data JPA的内部内容):

我可以通过简单地使用标准JPA方法DeleteById()轻松解决此问题,但是我想知道为什么定制的JPA方法DeletealBbyIdin()不会导致Hibernate搜索以更新Lucene索引。

Hibernate Search detects entity change events happening in your Hibernate ORM Session/EntityManager. This excludes insert/update/delete statements that you wrote yourself in JPQL or native SQL queries.

The limitation is documented here: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#limitations-changes-in-session

The workaround is documented there too:

One workaround is to reindex explicitly after you run JPQL/SQL queries, either using the MassIndexer or manually.

EDIT: And of course your workaround might be valid as well, if deleteById loads the entity in the session before deleting it (I'm not that familiar with the internals of Spring Data JPA):

I can easily solve this problem by simply using the standard jpa method deleteById() but i want to know why the custom made jpa method deleteAllByIdIn() does not cause Hibernate search to update the lucene index.

当对象通过@NorePositoryBean JPA方法删除对象时,Hibernate搜索不会从Lucene索引中删除旧值

云柯 2025-02-18 13:11:46

尝试一下

  <ArticleBoxes
      key={newsArticle.source.id}
      src={newsArticle.urlToImage}
      href={newsArticle.url}
      blogTitleAnchor={newsArticle.title}
    />

,我猜想如果您检查了控制台,您会发现 ID 不确定。您也需要为每个ID定义唯一ID,因为在数据中,大多数ID是 null

try this

  <ArticleBoxes
      key={newsArticle.source.id}
      src={newsArticle.urlToImage}
      href={newsArticle.url}
      blogTitleAnchor={newsArticle.title}
    />

I'm guessing if you checked your console you'd see that id is undefined. You need to define unique ids for each one too since in your data most of the ids are null

反应组件无法正确破坏JSON

云柯 2025-02-18 13:04:01

首先,我们首先需要一种创建安全的随机值以生成IV和键的方法。您还可以使用Cryptokit的 SymmetricKey 生成键并从中提取数据,但是现在,我将使用此功能。

extension Data {
    static func secureRandom(ofSize size: Int) -> Data {
        var output = [UInt8](repeating: 0, count: size)
        _ = SecRandomCopyBytes(kSecRandomDefault, size, &output)
        return Data(output)
    }
}

然后,我们需要计算AES CBC密文的可能性,可以使用CommonCrypto进行。

func encrypt(plaintext: Data, key: Data, iv: Data) -> Data {
    var encryptor: CCCryptorRef?
    defer {
        CCCryptorRelease(encryptor)
    }

    var key = Array(key)
    var iv = Array(iv)
    var plaintext = Array(plaintext)

    CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOperation(kCCOptionPKCS7Padding), &key, key.count, &iv, &encryptor)

    var outputBytes = [UInt8](repeating: 0, count: CCCryptorGetOutputLength(encryptor, plaintext.count, false))
    CCCryptorUpdate(encryptor, &plaintext, plaintext.count, &outputBytes, outputBytes.count, nil)

    var movedBytes = 0
    var finalBytes = [UInt8](repeating: 0, count: CCCryptorGetOutputLength(encryptor, 0, true))
    CCCryptorFinal(encryptor, &finalBytes, finalBytes.count, &movedBytes)

    return Data(outputBytes + finalBytes[0 ..< movedBytes])
}

以及具有SHA-256哈希功能的HMAC。我建议在此处使用Cryptokit的HMAC实现,但是为了使事情变得简单,我选择了CommonCrypto实施。

func computeHMAC(_ data: Data, using key: Data) -> Data {
    var data = Array(data)
    var key = Array(key)
    var macOut = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
    CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA256), &key, key.count, &data, data.count, &macOut)
    return Data(macOut)
}

这将所有这些融合到以下

let plaintext = Data("Hello world!".utf8)

let signingKey = Data.secureRandom(ofSize: kCCKeySizeAES128)
let cryptoKey = Data.secureRandom(ofSize: kCCKeySizeAES128)
let fernetKey = (signingKey + cryptoKey).base64EncodedString()

let version: [UInt8] = [0x80]
let timestamp: [UInt8] = {
    let timestamp = Int(Date().timeIntervalSince1970).bigEndian
    return withUnsafeBytes(of: timestamp, Array.init)
}()
let iv = Data.secureRandom(ofSize: kCCBlockSizeAES128)
let ciphertext = encrypt(plaintext: plaintext, key: cryptoKey, iv: iv)
let hmac = computeHMAC(version + timestamp + iv + ciphertext, using: signingKey)

let fernetToken = (version + timestamp + iv + ciphertext + hmac).base64EncodedString()

print("Fernet key: \(fernetKey)")
print("Fernet token: \(fernetToken)")

示例输出中,

Fernet key: 7EwFlYNKTGfj+2fSgL3AUqtrRqRs4D1TWNK7t2XbGJQ=
Fernet token: gAAAAABivCLM0y0poDtGOohT1yK4XTDJppYPJdu4fuDTZ5tb9P9KP5ACgX8aJq4imsSdbzOCcvY3Tueo4FYbwyG+ZugozILL+Q==

我们可以在Python中使用Cryptography.io的实现来使用它。

from cryptography.fernet import Fernet

key = b'7EwFlYNKTGfj+2fSgL3AUqtrRqRs4D1TWNK7t2XbGJQ='
token = b'gAAAAABivCLM0y0poDtGOohT1yK4XTDJppYPJdu4fuDTZ5tb9P9KP5ACgX8aJq4imsSdbzOCcvY3Tueo4FYbwyG+ZugozILL+Q=='

Fernet(key).decrypt(token)
# b'Hello world!'

To start, we first need a way to create secure random values to generate the IV and keys. You can also generate the keys using CryptoKit's SymmetricKey and extract the data from them, but for now, I'll use this function.

extension Data {
    static func secureRandom(ofSize size: Int) -> Data {
        var output = [UInt8](repeating: 0, count: size)
        _ = SecRandomCopyBytes(kSecRandomDefault, size, &output)
        return Data(output)
    }
}

We then require the possibility to compute the AES CBC ciphertext, which can be done using CommonCrypto.

func encrypt(plaintext: Data, key: Data, iv: Data) -> Data {
    var encryptor: CCCryptorRef?
    defer {
        CCCryptorRelease(encryptor)
    }

    var key = Array(key)
    var iv = Array(iv)
    var plaintext = Array(plaintext)

    CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOperation(kCCOptionPKCS7Padding), &key, key.count, &iv, &encryptor)

    var outputBytes = [UInt8](repeating: 0, count: CCCryptorGetOutputLength(encryptor, plaintext.count, false))
    CCCryptorUpdate(encryptor, &plaintext, plaintext.count, &outputBytes, outputBytes.count, nil)

    var movedBytes = 0
    var finalBytes = [UInt8](repeating: 0, count: CCCryptorGetOutputLength(encryptor, 0, true))
    CCCryptorFinal(encryptor, &finalBytes, finalBytes.count, &movedBytes)

    return Data(outputBytes + finalBytes[0 ..< movedBytes])
}

and the HMAC with the SHA-256 hash function. I recommend using CryptoKit's HMAC implementation here, but to keep things simple, I went with the CommonCrypto implementation.

func computeHMAC(_ data: Data, using key: Data) -> Data {
    var data = Array(data)
    var key = Array(key)
    var macOut = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
    CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA256), &key, key.count, &data, data.count, &macOut)
    return Data(macOut)
}

This brings all of this together into the following

let plaintext = Data("Hello world!".utf8)

let signingKey = Data.secureRandom(ofSize: kCCKeySizeAES128)
let cryptoKey = Data.secureRandom(ofSize: kCCKeySizeAES128)
let fernetKey = (signingKey + cryptoKey).base64EncodedString()

let version: [UInt8] = [0x80]
let timestamp: [UInt8] = {
    let timestamp = Int(Date().timeIntervalSince1970).bigEndian
    return withUnsafeBytes(of: timestamp, Array.init)
}()
let iv = Data.secureRandom(ofSize: kCCBlockSizeAES128)
let ciphertext = encrypt(plaintext: plaintext, key: cryptoKey, iv: iv)
let hmac = computeHMAC(version + timestamp + iv + ciphertext, using: signingKey)

let fernetToken = (version + timestamp + iv + ciphertext + hmac).base64EncodedString()

print("Fernet key: \(fernetKey)")
print("Fernet token: \(fernetToken)")

An example output can be

Fernet key: 7EwFlYNKTGfj+2fSgL3AUqtrRqRs4D1TWNK7t2XbGJQ=
Fernet token: gAAAAABivCLM0y0poDtGOohT1yK4XTDJppYPJdu4fuDTZ5tb9P9KP5ACgX8aJq4imsSdbzOCcvY3Tueo4FYbwyG+ZugozILL+Q==

We can use this in python using cryptography.io's implementation

from cryptography.fernet import Fernet

key = b'7EwFlYNKTGfj+2fSgL3AUqtrRqRs4D1TWNK7t2XbGJQ='
token = b'gAAAAABivCLM0y0poDtGOohT1yK4XTDJppYPJdu4fuDTZ5tb9P9KP5ACgX8aJq4imsSdbzOCcvY3Tueo4FYbwyG+ZugozILL+Q=='

Fernet(key).decrypt(token)
# b'Hello world!'

在Python和Swift之间交换加密消息

云柯 2025-02-18 08:25:34

我看起来您可能有一些不匹配的角度。您的标题说Angular版本4.4.7。您的树说Angular-CLI 6.2.9,您的打字稿版本在2.7.2上。对于角度和角度CLI,打字稿要求将有所不同。我会首先通过降低CLI或升级角度来对齐这些版本。然后擦除node_modules文件夹,然后安装新的NPM。 Angular/Angular-CLI升级的有用工具

I looks like you might have some mismatching versions of angular. Your title says Angular version 4.4.7. Your tree says angular-cli 6.2.9 and you typescript version on 2.7.2. Typescript requirements are going to be different for angular and angular-cli. I would align those versions first by either downgrading cli or upgrading angular. Then wipe your node_modules folder and do a fresh npm install. Helpful tool for angular/angular-cli upgrades HERE

Angular 4.4.7打字稿2.7.2获取TS1005:&#x27 ;;&#x27;

云柯 2025-02-17 19:03:18

使用 tidyverse 和a for循环:

library(tidyverse)

df <- data.frame(V1 = c("a","b","c"))

for(i in 1:3){
  
  print(paste("The values are",
        df |> 
          slice(1:i) |> 
          pull(V1) |> 
          str_c(collapse = " "),
        "now."))
}

输出:

[1] "The values are a now."
[1] "The values are a b now."
[1] "The values are a b c now."

Using tidyverse and a for loop:

library(tidyverse)

df <- data.frame(V1 = c("a","b","c"))

for(i in 1:3){
  
  print(paste("The values are",
        df |> 
          slice(1:i) |> 
          pull(V1) |> 
          str_c(collapse = " "),
        "now."))
}

Output:

[1] "The values are a now."
[1] "The values are a b now."
[1] "The values are a b c now."

循环创建句子,该句子反复在列表中添加一个额外的单词

云柯 2025-02-17 18:21:43

当您说

char *msg = "HI!";

几个特殊的事情正在发生时。首先,字符串文字“ hi!” 被视为匿名数组

char anonymous_array[] = "HI!";

(这种对字符串文字的处理是C语言 dos 支持的唯一方法 “字符串类型”的概念。)

第二,当您尝试使用此数组的值时,您得到的(就像在C中一样)是指向数组的第一个元素的指针:

char *msg = &anonymous_array[0];

而且效果很好。另一方面,当您

int *p = 2;

没有这样说时。如果有的话,这告诉编译器不是您想要一个指向整数2的指针> p ,而是您想要一个指针 p 指向内存地址2 。但是您的程序可能无法访问内存地址2,即使这样做,对于普通程序,也无法知道该地址的内容,您无能为力。 (另一方面,如果您进行嵌入式编程,您可能会知道,该地址在特定体系结构上有一些有趣的东西。)

如果您想要 p 指向带有值的整数对象2,您需要以某种方式创建该整数对象。您可以做

int i = 2;
int *p = &i;

int array[] = { 2 };
int *p = array;

或 - 这最终更接近字符串初始化的工作方式 - 您可以使用一种特殊的语法,称为a compound compound literal

int *p = (int []){ 2 };

When you say

char *msg = "HI!";

several special things are happening. First, the string literal "HI!" is treated like an anonymous array

char anonymous_array[] = "HI!";

(This treatment of string literals is one and perhaps the only way in which the C language does support the notion of a "string type".)

Second, when you attempt to use the value of this array, what you get (as you just about always do in C) is a pointer to the array's first element:

char *msg = &anonymous_array[0];

And that works well. On the other hand, when you say

int *p = 2;

nothing like that happens. If anything, this tells the compiler not that you want a pointer p pointing at the integer 2, but rather, that you want a pointer p that points at memory address 2. But your program probably doesn't have access to memory address 2, and even if it did, for a normal program, there's no way to know what's at that address, and nothing meaningful you could do with it. (If you're doing embedded programming, on the other hand, you might know that there's something interesting at that address for your particular architecture.)

If you want p to point to an integer object with the value 2, you'll need to create that integer object somehow. You could do

int i = 2;
int *p = &i;

or

int array[] = { 2 };
int *p = array;

Or — and this ends up being much closer to the way string initialization works — you can use a special syntax called a compound literal:

int *p = (int []){ 2 };

为什么char*直接设置该值,而在c编程中没有int*的情况下

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文